/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import entities.ProjColumn;
import java.net.URI;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entities.Project;

/**
 *
 * @author Alex
 */

@XmlRootElement(name = "projColumn")
public class ProjColumnConverter {
    private ProjColumn entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of ProjColumnConverter */
    public ProjColumnConverter() {
        entity = new ProjColumn();
    }

    /**
     * Creates a new instance of ProjColumnConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public ProjColumnConverter(ProjColumn entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getProjId();
    }

    /**
     * Creates a new instance of ProjColumnConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public ProjColumnConverter(ProjColumn entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for id.
     *
     * @return value for id
     */
    @XmlElement
    public Long getId() {
        return (expandLevel > 0) ? entity.getId() : null;
    }

    /**
     * Setter for id.
     *
     * @param value the value to set
     */
    public void setId(Long value) {
        entity.setId(value);
    }

    @XmlElement
    public Long getProjectId() {
        return (expandLevel > 0) ? entity.getProjectId() : null;
    }

    public void setProjectId(Long value) {
        entity.setProjectId(value);
    }

    /**
     * Getter for columnName.
     *
     * @return value for columnName
     */
    @XmlElement
    public String getColumnName() {
        return (expandLevel > 0) ? entity.getColumnName() : null;
    }

    /**
     * Setter for columnName.
     *
     * @param value the value to set
     */
    public void setColumnName(String value) {
        entity.setColumnName(value);
    }

    /**
     * Getter for columnDescription.
     *
     * @return value for columnDescription
     */
    @XmlElement
    public String getColumnDescription() {
        return (expandLevel > 0) ? entity.getColumnDescription() : null;
    }

    /**
     * Setter for columnDescription.
     *
     * @param value the value to set
     */
    public void setColumnDescription(String value) {
        entity.setColumnDescription(value);
    }

    /**
     * Getter for projId.
     *
     * @return value for projId
     */
    @XmlElement
    public ProjectConverter getProjId() {
        if (expandLevel > 0) {
            if (entity.getProjId() != null) {
                return new ProjectConverter(entity.getProjId(), uri.resolve("projId/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for projId.
     *
     * @param value the value to set
     */
    public void setProjId(ProjectConverter value) {
        entity.setProjId((value != null) ? value.getEntity() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the ProjColumn entity.
     *
     * @return an entity
     */
    @XmlTransient
    public ProjColumn getEntity() {
        if (entity.getId() == null) {
            ProjColumnConverter converter = UriResolver.getInstance().resolve(ProjColumnConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved ProjColumn entity.
     *
     * @return an resolved entity
     */
    public ProjColumn resolveEntity(EntityManager em) {
        Project projId = entity.getProjId();
        if (projId != null) {
            entity.setProjId(em.getReference(Project.class, projId.getProjId()));
        }
        return entity;
    }
}
